package zozopo.manual;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReleaseTB;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyAction extends ActionSupport implements Action ,SessionAware,ServletRequestAware{
	
	private Map<Object, Object> session;
	private String subject;
	private String lable;
	private String context;
	private String url;
	private String brand;
	private String secondbrand;
	private String kind;
	private String image;
	
	HttpServletRequest request;

	public String execute(){
		
		UserTB user = (UserTB) session.get("userInfo"); 
		//BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
		ReleaseOS releaseOS = new ReleaseOS();
		ReleaseTB release = new ReleaseTB();
		
		release = releaseOS.getSubjectById(id);
		
		if(!user.getUser_email().equals(release.getRelease_useremail()) && ('0' == user.getUser_limitflag().charAt(1))){
			releaseOS.releaseConnection();
			return NONE;
		}
		
		release.setRelease_ip(getIpAddr(request));
		release.setRelease_useremail(user.getUser_email());
		release.setRelease_subject(subject);
		release.setRelease_lable(lable);
		release.setRelease_kind(kind);
		release.setRelease_image(image);
		context = context.replaceAll("'", "\\\\'");
		
		//System.out.println(messageTemp);
		context = Validate.htmlLabelFilter(context);
		release.setRelease_context(context);
		release.setRelease_brand(brand);
		release.setRelease_secondbrand(secondbrand);
		release.setRelease_url(url);
		if(!releaseOS.upadteRelease(release)){
			releaseOS.releaseConnection();
			return ERROR;
		}
		
		releaseOS.releaseConnection();
		
		return SUCCESS;
	}
	
	public void validate() {
		
		if(lable == null || lable.trim().equals("")){
			lable="MANUAL.LABLE.OTHER";
		}
		
		UserTB user = (UserTB) session.get("userInfo"); 
		
		
		if(user == null){
			addFieldError("errormessage", "用户未登陆或超时请重新登陆,再进行发布!");
			return;
		}
		
		if(!user.getUser_state().equals("1")){
			addFieldError("errormessage", "用户未激活不能发布信息,请激活后再进行发布!");
			return;
		}
		
		if(subject.length() < 5){
			addFieldError("errormessage", "标题长度不能少于5个字符.请重新输入!");
			return;
		}
		
		if(Validate.isSpecialCharacters(subject)){
			addFieldError("errormessage", "标题长度不能包含特殊字符  ;\"'\\*&`+-%?!<>[]{}|!");
			return;
		}
		if(Validate.isSpecialCharacters(brand)){
			addFieldError("errormessage", "品牌长度不能包含特殊字符  ;\"'\\*&`+-%?!<>[]{}|!");
			return;
		}
	}
	
	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getLable() {
		return lable;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}



	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	/**
     * 获得客户端真是ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getRemoteAddr();    
     }
		return ip; 
    }

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}
	
	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

	private int id;
	

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSecondbrand() {
		return secondbrand;
	}

	public void setSecondbrand(String secondbrand) {
		this.secondbrand = secondbrand;
	}
	
}
